﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace theRift
{
    public class Transformation
    {
        protected Vector3 translate;
        protected Vector3 rotate;
        protected Vector3 scale;
        protected Vector3 up;
        protected Vector3 right;

        protected bool needUpdate;

        protected Matrix matrix;

        public Vector3 Translate
        {
            get { return translate; }
            set { translate = value; needUpdate = true; }
        }

        public Vector3 Rotate
        {
            get { return rotate; }
            set { rotate = value; needUpdate = true; }
        }

        public Vector3 Scale
        {
            get { return scale; }
            set { scale = value; needUpdate = true; }
        }

        public Vector3 Up
        {
            get { return up; }
            set { up = value; }
        }

        public Vector3 Right
        {
            get { return right; }
            set { right = value; }
        }

        public Matrix Matrix
        {
            get
            {
                if (needUpdate)
                {
                    matrix = Matrix.Identity;                    
                    matrix.Forward = rotate;
                    matrix.Up = up;
                    matrix.Right = right;

                    // Scale before translation so we don't end up
                    // scaling our movement!!
                    matrix *= Matrix.CreateScale(scale);
                    matrix.Translation = translate;

                    needUpdate = false;
                }

                return matrix;
            }
        }

        public Transformation()
        {
            this.Translate = new Vector3(0.0f, 0.0f, 0.0f);
            this.Rotate = Vector3.Forward;
            this.Scale = new Vector3(1.0f, 1.0f, 1.0f);
            this.Right = Vector3.Right;
            this.Up = Vector3.Up;
        }
    }
}
